package com.ztgf.order.api.cms.purchase;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.common.utils.DateUtils;
import com.ztgf.common.utils.ExcelUtils;
import com.ztgf.common.utils.FileUtils;
import com.ztgf.file.models.bo.FileBO;
import com.ztgf.file.models.bo.FileInfoSaveBO;
import com.ztgf.order.models.bo.purchase.*;
import com.ztgf.order.models.service.purchase.IPurchaseInfoService;
import com.ztgf.order.models.vo.purchase.PurchaseCommodityBargainVO;
import com.ztgf.order.models.vo.purchase.PurchaseInfoExportVO;
import com.ztgf.order.models.vo.purchase.PurchaseInfoVO;
import com.ztgf.shopping.models.service.shop.IShopInfoService;
import com.ztgf.sys.models.service.ISysConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 采购信息表控制器
 *
 * @author mxli
 * @Date 2021-05-17 10:15:52
 */

@RestController
@RequestMapping("/cms/purchaseInfo")
@Slf4j
@Api(value = "PurchaseInfoController",tags = "采购信息表接口")
public class PurchaseInfoController {

    @DubboReference
    private IPurchaseInfoService purchaseInfoService;

    @DubboReference
    private ISysConfigService sysConfigService;

    @DubboReference
    private IShopInfoService shopInfoService;


    @ApiOperation(value = "添加采购信息表",notes = "添加采购信息表")
    @PostMapping(value = "/insert")
    public Result insert(PurchaseInfoSaveBO saveBO) {
        log.error("---PurchaseInfoSaveBO---" +JSON.toJSONString(saveBO));
        purchaseInfoService.add(saveBO);
        return new Result<>();
    }

    @PostMapping("/saveOwnBargain")
    @ApiOperation(value = "平台议价",notes = "平台议价")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "purchaseCommodityBargainId",value = "报价商品主键id",required = true,paramType = "query", dataType = "Long")
            ,@ApiImplicitParam(name = "price",value = "价格，2位小数",required = true,paramType = "query", dataType = "BigDecimal")
    })
    public Result saveOwnBargain(Long purchaseCommodityBargainId, BigDecimal price){
        return  purchaseInfoService.saveOwnBargain(purchaseCommodityBargainId, price);
    }

    @PostMapping("/saveOwnRefuseBargain")
    @ApiOperation(value = "平台拒绝报价",notes = "平台拒绝报价")
    @ApiImplicitParam(name = "purchaseCommodityBargainIds", value = "报价商品ids:以逗号隔开", required = true, paramType = "query", dataType = "String")
    public Result saveOwnRefuseBargain(String purchaseCommodityBargainIds){
        return  purchaseInfoService.saveOwnRefuseBargain(purchaseCommodityBargainIds);
    }

    @PostMapping("/saveOwnCancelBargain")
    @ApiOperation(value = "取消报价",notes = "取消报价")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids",value = "ids,以逗号分隔",required = true,paramType = "query", dataType = "String")
            ,@ApiImplicitParam(name = "canceType",value = "参照数据字典类型",required = true,paramType = "query", dataType = "Integer")
            ,@ApiImplicitParam(name = "canceDescription",value = "取消说明",required = true,paramType = "query", dataType = "String")
    })
    public Result saveOwnCancelBargain(String ids, Integer cancelType, String cancelDescription){
        return  purchaseInfoService.saveOwnCancelBargain(ids, cancelType, cancelDescription);
    }

    @PostMapping("/savePlatBargain")
    @ApiOperation(value = "平台砍价",notes = "平台砍价")
    @ApiImplicitParam(name = "purchaseCommodityBargainId", value = "砍价商品id", required = true, paramType = "query", dataType = "Long")
    public Result savePlatBargain(Long purchaseCommodityBargainId){
        return  purchaseInfoService.savePlatBargain(purchaseCommodityBargainId);
    }

    @PostMapping("/updateOrganiser")
    @ApiOperation(value = "修改主办人",notes = "修改主办人")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "purchaseId",value = "采购单id",required = true,paramType = "query", dataType = "Long")
            ,@ApiImplicitParam(name = "sysUserId",value = "系统用户id",required = true,paramType = "query", dataType = "Long")
    })
    public Result updateOrganiser(Long purchaseId, Long sysUserId){
        return  purchaseInfoService.updateOrganiser(purchaseId, sysUserId);
    }


    @ApiOperation(value = "首页查询报价列表",notes = "首页查询报价列表")
    @PostMapping("/queryHomePageList")
    public Result<Page<PurchaseInfoVO>> queryHomePageList(PurchaseInfoHomeBO bo){
        return purchaseInfoService.queryCmsHomePageList(bo);
    }

    @ApiOperation(value = "平台查询自己发布报价列表",notes = "平台查询自己发布报价列表")
    @PostMapping("/queryOwnPageList")
    public Result<Page<PurchaseInfoVO>> queryOwnPageList(PurchaseInfoOwnBO bo){
        return purchaseInfoService.queryOwnPageList(bo);
    }

    @PostMapping("/getOwn")
    @ApiOperation(value = "平台查询自己发布报价详情",notes = "平台查询自己发布报价详情")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<PurchaseInfoVO> getOwn(Long id){
        return  purchaseInfoService.getOwn(id);
    }

    @ApiOperation(value = "admin查询发布报价列表",notes = "admin查询发布报价列表")
    @PostMapping("/queryAdminPageList")
    public Result<Page<PurchaseInfoVO>> queryAdminPageList(PurchaseInfoAdminBO bo){
        return purchaseInfoService.queryAdminPageList(bo);
    }

    @PostMapping("/getAdmin")
    @ApiOperation(value = "admin查询报价详情",notes = "admin查询报价详情")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<PurchaseInfoVO> getAdmin(Long id){
        return  purchaseInfoService.getAdmin(id);
    }


    @ApiOperation(value = "admin查询平台砍价列表",notes = "admin查询平台砍价列表")
    @PostMapping("/queryPlatBargainPageList")
    public Result<Page<PurchaseInfoVO>> queryPlatBargainPageList(PurchaseInfoPlatBargainBO bo){
        return purchaseInfoService.queryPlatBargainPageList(bo);
    }

    @ApiOperation(value = "admin查询平台砍价详情",notes = "admin查询平台砍价详情")
    @PostMapping("/getPlatBargain")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<PurchaseInfoVO> getPlatBargain(Long id){
        return purchaseInfoService.getPlatBargain(id);
    }


    @ApiOperation(value = "代客下单",notes = "代客下单")
    @PostMapping("/saveAgentOrder")
    @ApiImplicitParam(name = "purchaseCommodityBargainIds",value = "报价商品ids，逗号分隔",required = true,paramType = "query", dataType = "String")
    public Result saveAgentOrder(String purchaseCommodityBargainIds){
        return purchaseInfoService.saveAgentOrder(purchaseCommodityBargainIds);
    }

    @PostMapping("/queryCommodityBargainList")
    @ApiOperation(value = "根据id报价商品详情列表",notes = "根据id报价商品详情列表")
    @ApiImplicitParam(name = "purchaseCommodityBargainIds",value = "报价商品ids，逗号分隔",required = true,paramType = "query", dataType = "String")
    public Result<List<PurchaseCommodityBargainVO>> queryCommodityBargainList(String purchaseCommodityBargainIds){
        return  purchaseInfoService.queryCommodityBargainList(purchaseCommodityBargainIds);
    }


    @ApiOperation(value = "导出平台发布报价单列表",notes = "导出买家报价单列表")
    @PostMapping("/exportOwnList")
    public void exportOwnList(HttpServletResponse response,PurchaseInfoOwnBO bo){
        List<PurchaseInfoExportVO> purchaseInfoExportVOS = purchaseInfoService.queryExportOwnList (bo);
        ExcelUtils.exportExcel(purchaseInfoExportVOS,"询价单列表","询价单列表", PurchaseInfoExportVO.class,"询价单列表"+ DateUtils.dateToStr(new Date (), DateUtils.DATE_FORMAT_DATESHORTDAY) +".xlsx",response);
    }


    @ApiOperation(value = "导出admin报价单列表",notes = "导出admin报价单列表")
    @PostMapping("/exportAdminList")
    public void exportAdminList(HttpServletResponse response,PurchaseInfoAdminBO bo){
        List<PurchaseInfoExportVO> purchaseInfoExportVOS = purchaseInfoService.queryExportAdminList (bo);
        ExcelUtils.exportExcel(purchaseInfoExportVOS,"询价单列表","询价单列表", PurchaseInfoExportVO.class,"询价单列表"+ DateUtils.dateToStr(new Date (), DateUtils.DATE_FORMAT_DATESHORTDAY) +".xlsx",response);
    }

    @ApiOperation(value = "admin导出平台砍价列表",notes = "admin导出平台砍价列表")
    @PostMapping("/exportPlatBargainList")
    public void exportPlatBargainList(HttpServletResponse response,PurchaseInfoPlatBargainBO bo){
        List<PurchaseInfoExportVO> purchaseInfoExportVOS = purchaseInfoService.queryExportPlatBargainList (bo);
        ExcelUtils.exportExcel(purchaseInfoExportVOS,"询价单列表","询价单列表", PurchaseInfoExportVO.class,"询价单列表"+ DateUtils.dateToStr(new Date (), DateUtils.DATE_FORMAT_DATESHORTDAY) +".xlsx",response);
    }


    @PostMapping("/getCommodityBargain")
    @ApiOperation(value = "根据id报价商品详情",notes = "根据id报价商品详情")
    @ApiImplicitParam(name = "id",value = "报价商品表主键id",required = true,paramType = "query", dataType = "Long")
    public Result<PurchaseCommodityBargainVO> getCommodityBargain(Long id,Long brandId,Long shopId){
        return  purchaseInfoService.getCommodityBargain(id,brandId,shopId);
    }


    @PostMapping("/getPurchaseInfo")
    @ApiOperation(value = "查询询价点信息（相似商品）",notes = "查询询价点信息（相似商品）")
    @ApiImplicitParam(name = "id",value = "报价单主键id",required = true,paramType = "query", dataType = "Long")
    public Result<PurchaseInfoVO> getPurchaseInfo(Long id){
        return  purchaseInfoService.getPurchaseInfo(id);
    }

    @PostMapping("/importPurchase")
    @ApiOperation(value = "admin导入询价单",notes = "admin导入询价单")
    @SneakyThrows
    public Result importPurchase(PurchaseInfoImportSaveBO saveBO, @RequestParam("file") MultipartFile file){
        if (null == file || file.isEmpty()) {
            return new Result<>(CodeEnum.FAIL_FILE_400000);
        }
        byte[] input = file.getBytes();
        log.info ("解析input---------------1" + input);
        String originalFilename = file.getOriginalFilename();
        originalFilename = FileUtils.checkCompatible(originalFilename);
        String fileName = FileUtils.getFileName(originalFilename);
        String fileExtName = FileUtils.getFileExtName(originalFilename);
        FileBO fileBO = new FileBO();
        fileBO.setFileName(fileName);
        fileBO.setFileExtName(fileExtName);
        fileBO.setInputs(input);
        fileBO.setFileSize(file.getSize());
        List<FileBO> list = new ArrayList<FileBO>();
        list.add(fileBO);
        FileInfoSaveBO fileInfoBO = new FileInfoSaveBO();
        fileInfoBO.setFileBOS(list);
        fileInfoBO.setShopId(saveBO.getShopId());
        fileInfoBO.setPlatId(saveBO.getCreateUserId());
        fileInfoBO.setBusinessType(9);//上传的excel模板类型
        return  purchaseInfoService.saveImportPurchase(saveBO,input,fileInfoBO);
    }



}
